 function add2list(){
  if(isMutiOperator()){
    //tp1.getSelectedIndex()是判断是否在组织结构中选择
    if(tp1.getSelectedIndex()==0){  //从环节定义人员中选择
      preSelect2List();
    }
    else{
      addOrgObject2List();  //从组织结构中选择
    }
  }
  else{
      alert("只能选择一个人员")
  }
}


function addTolist(){
  if(isMutiOperator()){
      preSelect2List();
  } else{
      alert("只能选择一个人员")
  }
}


//组织结构树中事件映射
function addOrgObject(){
  add2list()
}
//
function checkOrgNode(AOrgBCFL)
{
  var strCurCode = "";
  for (i=0;i<arOrgBCFL.length;i++)
  {
    strCurCode = arOrgBCFL[i];
    if(strCurCode==AOrgBCFL||
    (AOrgBCFL.length>strCurCode.length&&AOrgBCFL.substring(0,strCurCode.length)==strCurCode))
    {
      return true;
    }

  }
  return false;
}

function addOrgObject2List()
{
 var orgCode=jsOrgVO.orgCode
 var orgName=jsOrgVO.orgName
 var orgType=jsOrgVO.orgType

 var flag;
 if (!checkOrgNode(jsOrgVO.orgBCFL)) return ;

 if(orgCode!="" && orgName!="" && orgType!="")
 {

  for(i=0;i<tableOrgList.rows.length;i++)
  {
    if(orgCode==tableOrgList.rows[i].cells[0].id)
    {
      flag=true;
      break;
    }
  }

  if(!flag){
    orgs.addOrg("",orgName,"",orgType,orgCode,"");
    var newRow = tableOrgList.insertRow()
    var newCell = newRow.insertCell()
    newCell.innerHTML = "<P class='tdFontStyleSmall' onDblClick='javascript:delOrgObject()'>" + orgName + "</P>"
    newCell.id=orgCode
   }
 }
}

function preSelect2List(){   //将树型结构中选定的项加到list中
  var orgCode="";
  var orgBCFL="";
  var orgName="";
  var orgType="";
  var petityDusty="";
  var flgPetityDusty="";

  var temp
  var flag;
  var nodeOperatorArray = document.getElementsByName("nodeOperators");
  for (i=0;i<nodeOperatorArray.length;i++){
   flag=false;
   temp = nodeOperatorArray[i]

   if(temp.checked){
     orgCode = temp.value.split("#")[0];
     orgName = temp.value.split("#")[1];
     orgType = temp.value.split("#")[2];
     orgBCFL = temp.value.split("#")[3];
     //获取环节操作员的次责任
     petityDusty = document.getElementById(orgCode);
     if(petityDusty.checked){
        flgPetityDusty="Y";  //次责任
     }else{
        flgPetityDusty="N"; //主责任
     }
     
     var nbsp=orgName.lastIndexOf(" ");
     if(nbsp>0){
       orgName=orgName.substring(1);
     }
     
     if(orgCode!="" && orgName!="" && orgType!=""){
       var tableOrgListCount = tableOrgList.rows.length
       for(k=0;k<tableOrgListCount;k++){
         if(orgCode == tableOrgList.rows[k].cells[0].id){
           flag=true;
           break;
         }
       }

       if(!flag){
         orgs.addOrg(orgBCFL,orgName,"",orgType,orgCode,flgPetityDusty,"");
         var newRow = tableOrgList.insertRow();
         var newCell = newRow.insertCell();
         if(flgPetityDusty=="Y"){
           orgName=orgName+"(次责)";
         }
         newCell.innerHTML = "<P class='tdFontStyleSmall' onDblClick='javascript:delOrgObject()'>" + orgName + "</P>";
         newCell.id=orgCode;
       }
     }
   }
  }
}

function delOrgObject(){
  var selectedRows = tableSelected.getItems();
  if (selectedRows != null){
    for (i=0;i<selectedRows.length;i++){
      if (tableSelected.getItemSelected(selectedRows[i])){
        orgs.delOrg(selectedRows[i].id);   //将当前选顶的人员从已经选定人员对象中删除
        //从表格中删除
        tableOrgList.deleteRow(tableSelected.getItemIndex(selectedRows[i]))
      }
    }
  }
}

//在已经选择的人员列表中调整顺序
//向上移动
function up()
{
 //********************************//
   //处理策略，当有多个选择时，取最前面的
 //*********************************//
  try{
  //当前选定的行的索引
 var selIndex

 //当已选定框为空时退出

 if (tableOrgList.rows.length == 0){
   return
 }

 //当没有选择移动行时退出

 if (tableSelected.getSelectedIndexes() == null || tableSelected.getSelectedIndexes() == ""){
   return
 }

 //已经选定的所有的行的索引数组
 var allSelectedIndex = tableSelected.getSelectedIndexes()

 if (allSelectedIndex == null){
   return
 }
 if (allSelectedIndex[0] <1){
   return
 }
 selIndex = allSelectedIndex[0]
 //将所有的选定的行取消选定
  for (i=0;i<allSelectedIndex.length;i++){
    if (allSelectedIndex[i] != selIndex){
      //真实内容
      tableSelected.setItemSelected(tableSelected.getItem(allSelectedIndex[i]),false)
      //界面
      tableSelected.setItemSelectedUi(tableSelected.getItem(allSelectedIndex[i]),false)
    }
  }

   var currentRow = tableSelected.getItem(selIndex);  //当前将要移动的行
   var tmpCode=currentRow.id;
   var tmpName=currentRow.innerText;
   var preRow = tableSelected.getPrevious(tableSelected.getItem(selIndex));    //当前行的前一行

   var tempId = preRow.id
   var tempHTML = preRow.innerHTML

   orgs.permutation(tmpCode,preRow.id);

   preRow = currentRow;

   //将数据写到表格中
   //javascript中的对象传递是指针
   tableOrgList.rows[selIndex-1].cells[0].id = preRow.id
   tableOrgList.rows[selIndex-1].cells[0].innerHTML  = preRow.innerHTML

   tableOrgList.rows[selIndex].cells[0].id = tempId
   tableOrgList.rows[selIndex].cells[0].innerHTML  = tempHTML

  //真实内容
   tableSelected.setItemSelected(tableSelected.getItem(selIndex),false)
   //界面
   tableSelected.setItemSelectedUi(tableSelected.getItem(selIndex),false)

  //真实内容
   tableSelected.setItemSelected(tableSelected.getItem(selIndex-1),true)
   //界面
   tableSelected.setItemSelectedUi(tableSelected.getItem(selIndex-1),true)
 }
 catch (err){

 }
}

function down()
{
 //********************************//
   //处理策略，当有多个选择时，取最前面的
 //*********************************//
 try{
  //当前选定的行的索引
 var selIndex


 //当已选定框为空时退出
 if (tableOrgList.rows.length == 0){
   return
 }

 //当没有选择移动行时退出

 if (tableSelected.getSelectedIndexes() == null || tableSelected.getSelectedIndexes() == ""){
   return
 }
 //已经选定的所有的行的索引数组
 var allSelectedIndex = tableSelected.getSelectedIndexes()

 if (allSelectedIndex == null){
   return
 }

 selIndex = allSelectedIndex[0]

//假如是最后一行，不需要向下移动
 if (tableSelected.getNext(tableSelected.getItem(selIndex)) == null){
   return
 }

 //将所有的选定的行取消选定
  for (i=0;i<allSelectedIndex.length;i++){
    if (allSelectedIndex[i] != selIndex){
      //真实内容
      tableSelected.setItemSelected(tableSelected.getItem(allSelectedIndex[i]),false)
      //界面
      tableSelected.setItemSelectedUi(tableSelected.getItem(allSelectedIndex[i]),false)
    }
  }

   var currentRow = tableSelected.getItem(selIndex);  //当前将要移动的行
   var tmpCode=currentRow.id;
   var tmpName=currentRow.innerText;
   var NextRow = tableSelected.getNext(tableSelected.getItem(selIndex));    //当前行的后一行

   var tempId = NextRow.id
   var tempHTML = NextRow.innerHTML

   orgs.permutation(tmpCode,NextRow.id);

   NextRow = currentRow;

   //将数据写到表格中
   //javascript中的对象传递是指针
   tableOrgList.rows[selIndex+1].cells[0].id = NextRow.id
   tableOrgList.rows[selIndex+1].cells[0].innerHTML  = NextRow.innerHTML

   tableOrgList.rows[selIndex].cells[0].id = tempId
   tableOrgList.rows[selIndex].cells[0].innerHTML  = tempHTML

  //真实内容
   tableSelected.setItemSelected(tableSelected.getItem(selIndex),false)
   //界面
   tableSelected.setItemSelectedUi(tableSelected.getItem(selIndex),false)

  //真实内容
   tableSelected.setItemSelected(tableSelected.getItem(selIndex+1),true)
   //界面
   tableSelected.setItemSelectedUi(tableSelected.getItem(selIndex+1),true)
 }
 catch (err){

 }
}
